﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.Samples.Office.UnifiedCommunications.Gadget.DTO;
using Microsoft.Samples.Office.UnifiedCommunications.Gadget.Events;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget
{
    public partial class PresenceHistory : Window
    {
        #region Variables

        private static PresenceHistory _instance = null;
        private static int _instanceCount = 0;

        #endregion

        #region Properties

        public static int InstanceCount { get { return _instanceCount; } }

        public static PresenceHistory Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PresenceHistory();
                    _instance.InitializeComponent();
                }
                return _instance;
            }
        }

        public DesktopContact ContactToDisplay
        {
            get { return (DesktopContact)GetValue(ContactToDisplayProperty); }
            set { SetValue(ContactToDisplayProperty, value); }
        }

        public static readonly DependencyProperty ContactToDisplayProperty =
            DependencyProperty.Register(
                "ContactToDisplay",
                typeof(DesktopContact),
                typeof(PresenceHistory),
                new FrameworkPropertyMetadata(new PropertyChangedCallback(OnContactToDisplayChanged)));

        private static void OnContactToDisplayChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            (o as PresenceHistory).InitializeDesktopContact(e.NewValue as DesktopContact);
        }

        #endregion

        public PresenceHistory()
        {
            _instance = this;

            _instanceCount++;

            InitializeCommands();
            InitializeComponent();

            this.MouseDown += new MouseButtonEventHandler(PresenceHistory_MouseDown);
            this.Unloaded += new RoutedEventHandler(PresenceHistory_Unloaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(PresenceHistory_Closing);

            GadgetEventManager.Instance.ApplicationUnloadedEvent += new EventHandler(Instance_ApplicationUnloadedEvent);

            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }

        #region Loading

        void Window_Loaded(object sender, RoutedEventArgs e) {}

        void InitializeDesktopContact(DesktopContact desktopContact)
        {
            txtName.Text = desktopContact.DisplayName;
            this.Title = String.Format("Presence History for {0}", desktopContact.DisplayName);
        }

        void LoadPresenceHistory()
        {
            listBoxPresenceEvents.ItemsSource = new PresenceEventCollection(this.ContactToDisplay.SipUri);
        }

        private void tbRefresh_MouseDown(object sender, MouseButtonEventArgs e)
        {
            LoadPresenceHistory();
        }

        #endregion

        #region Unloading

        void PresenceHistory_Unloaded(object sender, RoutedEventArgs e)
        {
            Gadget.Instance.PresenceHistoryWindows.Remove(this.ContactToDisplay.SipUri);

            this.MouseDown -= new MouseButtonEventHandler(PresenceHistory_MouseDown);
            this.Closing -= new System.ComponentModel.CancelEventHandler(PresenceHistory_Closing);

            GadgetEventManager.Instance.ApplicationUnloadedEvent -= new EventHandler(Instance_ApplicationUnloadedEvent);
        }

        void Instance_ApplicationUnloadedEvent(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Other UI Functionality

        void PresenceHistory_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch
            {

            }
        }

        void PresenceHistory_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _instanceCount--;
        }

        void BackDoubleAnimation_Completed(object sender, EventArgs e)
        {
            LoadPresenceHistory();
        }

        void InitializeCommands()
        {
            CommandBindings.Add(new CommandBinding(CloseWindow, CloseWindowExecuted));
            CommandBindings.Add(new CommandBinding(ToggleMinimizedState, MinimizeWindowExecuted));
            CommandBindings.Add(new CommandBinding(ToggleMaximizedState, MaximizeWindowExecuted));
        }

        public static RoutedUICommand CloseWindow = new RoutedUICommand("Close", "CloseWindow", typeof(PresenceHistory));

        private void CloseWindowExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            Close();
        }

        public static RoutedUICommand ToggleMaximizedState = new RoutedUICommand("Maximize", "ToggleMaximizedState", typeof(PresenceHistory));

        private void MaximizeWindowExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            PresenceHistory.Instance.WindowState =
                (PresenceHistory.Instance.WindowState == WindowState.Maximized) ? WindowState.Normal : WindowState.Maximized;
        }

        public static RoutedUICommand ToggleMinimizedState = new RoutedUICommand("Minimize", "ToggleMinimizedState", typeof(PresenceHistory));

        private void MinimizeWindowExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            PresenceHistory.Instance.WindowState =
                (PresenceHistory.Instance.WindowState == WindowState.Minimized) ? WindowState.Normal : WindowState.Minimized;
        }

        #endregion
    }
}
